// 根据后台返回的路径，生成页面的组件模版
// function loadView(component) {
//   return (resolve) => require([`@/views/${component}`], resolve);
// }

export function formatRoutes(routes, formatRoutesArr = []) {
  routes.forEach((route) => {
    const { type, menuName, path, name, children } = route;
    if (type === "menu") {
      const router = {
        meta: {},
      };
      router["name"] = menuName;
      router["meta"]["title"] = name;
      const tempPath = path.replace("/app-vue", "");
      router["path"] = tempPath;
      router["component"] = (resolve) => {
        // 拼出相对路径，由于component无法识别变量
        require(["@/views" + tempPath], resolve);
      };
      formatRoutesArr.push(router);
    }
    if (children && children instanceof Array && children.length > 0) {
      formatRoutes(children, formatRoutesArr);
    }
  });
  return formatRoutesArr;
}

export function clone(source) {
  var result;
  if (Object.prototype.toString.call(source) === "[object Object]") {
    result = {};
  } else if (Object.prototype.toString.call(source) === "[object Array]") {
    result = [];
  } else {
    return;
  }
  for (var attr in source) {
    if (
      Object.prototype.toString.call(source[attr]) === "[object Array]" ||
      Object.prototype.toString.call(source[attr]) === "[object Object]"
    ) {
      result[attr] = clone(source[attr]);
    } else {
      result[attr] = source[attr];
    }
  }
  return result;
}
